<?php
namespace Admin\Controller;
use Think\Controller;
class SpecialController extends Controller {

	// 创新专题列表
	public function showList() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 3 ); // 验证是否登陆
		$number = 1; // 计数器，前台编号累加值
		$count=M('special')->count();
		$page=getPage($count,10);
		$list=M()->table(array('special'=>'special','projectcategory'=>'projectcategory'))->where('special.pcid=projectcategory.id')->limit($page->firstRow,$page->listRows)->select();
		$this->assign("list",$list);
		$this->assign("number",$number);	
		$this->display ();
	}
	
	// 发布专题
	public function addSpecial() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 3 ); // 验证是否登陆
		$number = 1; // 计数器，前台编号累加值
        if (! empty ( $_POST )) {
        	$showList = M ( 'special' ); // 实例化special
        	$data['sname']=I('post.name');
        	$data['sdate']=date('Y-m-d H:i:s');
        	$data['coursedate']=I('post.teatime');
        	$data['pcid']=I('post.cid');
        	$data['majorteacher']=I('post.major');
        	if ($showList->add($data)) {
        		$Hint->mysuccee ( "提交成功", "2", "Admin/Special/showList" );
				die ();
        	}
        	else{
        		$Hint->myerror ( "提交失败", "2", "Admin/Special/showList" );
				die ();
        	}
        }
		
		else{
			$this->display ();
		}
		
	}
	
	// 修改专题
	public function alterSpecial() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 3 ); // 验证是否登陆
		$id=$_GET['id'];
		$where['sid']=$id;
		$map['id']=$id;
		$number = 1; // 计数器，前台编号累加值
		$showList = M ( 'special' ); // 实例化special
		$projectcategory=M('projectcategory');
		
		if (! empty ( $_POST )) {
		$data['sname']=I('post.sname');
		$data['coursedate']=I('post.coursedate');
		$data['pcid']=I('post.pcid');
		$data['majorteacher']=I('post.majorteacher');
		$re=$showList->where($where)->save($data);
		if ($re) {
			$Hint->mysuccee("修改成功","2","Admin/Special/showList");
		}
		else{
			$Hint->myerror("修改失败","2");

		}
		}
	else{
		$list=$showList->where($where)->select();
		$cate=$projectcategory->where($map)->getfield('name');
		$this->assign("list",$list);
		$this->assign("cate",$cate);
		$this->display ();
	}
	}
	// 删除专题
	public function delSpecial($id) {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 3 ); // 验证是否登陆
		$number = 1; // 计数器，前台编号累加值
		$where['sid']=$id;
		$showList = M ( 'special' );
		$specialcourse=M('specialcourse');
		$re=$specialcourse->where($where)->select();
		try{
			if ($re==null) {
			
        $showList = M ( 'special' ); // 实例化specialfield('a.scid,a.scname,a.startdate,a.enddate,b.sname,b.majorteacher')->
		if ($showList->delete($id)) {
			$this->success("删除成功",U('showList'));
		}else{
			$this->error("删除失败");
		}
			}

			else{
					throw new \Think\Exception('这个专题下还有课程，如要删除，请先删除其全部课程');
			}
		}catch (\Exception $e) {
			/*echo $e->getMessage();
			echo "海马啊";*/
			$Hint->myerror("".$e->getMessage(),"2","showList");
		}

		
	}
public function checkcourse($re)
{
   if ($re==null) {
   $showList = M ( 'special' ); // 实例化specialfield('a.scid,a.scname,a.startdate,a.enddate,b.sname,b.majorteacher')->
		if ($showList->delete($id)) {
			$this->success("删除成功",U('showList'));
		}else{
			$this->error("删除失败");
		}
   }
   else{
   	//throw new Exception("这个专题下还有课程，如要删除，请先删除其全部课程");
   	throw new \Think\Exception('这个专题下还有课程，如要删除，请先删除其全部课程');
   }
}
	public function detial($sid){
		
		$weekarray=array("日","一","二","三","四","五","六");
		$courselist=D('specialcourse');
		$special= D('special')->find($sid);
		$Res=$courselist->where(array('sid'=>$sid))->select();
		foreach ($Res as $k => $v) {
			$time=strtotime($v['startdate']);
			$Res[$k]['week']="星期".$weekarray[date("w",$time)];
			$now=time();
			if(strtotime($v['startdate'])>$now){
				$Res[$k]['status']="还未开始...";
			}
			if(strtotime($v['enddate'])<$now){
				$Res[$k]['status']="已结束...";
			}
			if(strtotime($v['startdate'])<$now && strtotime($v['enddate'])>$now){
				$Res[$k]['status']="正在进行...";
			}
			  $intro=$courselist->where(array('sid'=>$sid))->getfield('content');
			    $str = strip_tags ( $intro ); // 过滤html标签
				$str = str_replace ( " ", "", "$str" ); // 过滤空格
				$result = mb_substr ( $str, 0, 50, 'utf-8' ); // 截取内容
				$Res [$k] ['content'] = $result;
		}
		$this->assign(array('special'=>$special,'Res'=>$Res));
		$this->display();
	}

  //   public function saveresult($sid){
  //   	$data=$_GET;
		// $activity=D('specialcourse');
		// $res=$activity->where(array('sid'=>$sid))->save($data);
		// if ($res) {
		// 	echo "1";
		// }else{
		// 	echo "0";
		// }

  //   }









}